<?php 
    include_once('./public/header.php');
    $res = select($config,'article','as a left join user as u on a.uid=u.id where a.id='.$_GET['id'],'',
                 'a.title,u.nicname as uid,a.content,a.ctime,a.clicknum,a.id as id');
    $cli['clicknum'] = $res[0]['clicknum'] + 1;
    $cli['id'] = $res[0]['id'];
    update($config,'article',$cli);
    $reply = dispage($config,'reply','as r left join user as u on r.uid=u.id where r.aid='.$_GET['id'],
                 'r.ctime,r.reply,u.nicname,u.pic,r.uid,r.id as id');
    $num = $reply[5];
    $a = $reply[4];
?>
<ul>
    <li><a href="/home/index.php"><span>Home Page</span></a></li>
    <li><a href="/home/login/register.php"><span>Register</span></a></li>
    <li><a href="/home/login/login.php"><span>LogIn</span></a></li>
    <li><a href="/home/login/updatepwd.php"><span>Update Pwd</span></a></li>
    <li><a href="/home/login/logoff.php"><span>LogOff</span></a></li>
<?php
    if(!empty($_SESSION)){
        echo '<li><a href=""><span>welcome: '.$_SESSION['uname'].'</span></a></li>';
    }
?>
</ul>
<?php include_once('./public/middle.php');?>
  <div class="content">
    <div class="content_resize">
      <div class="mainbar">
        <?php
            echo '<div class="article">';
            echo '<h2>'.$res[0]['title'].'</h2>';
            echo '<div class="clr"></div>';
            echo '<p>Posted by <a href="#">'.$res[0]['uid'].'</a>';
            echo '<p>'.$res[0]['content'].'</p>';
            echo '<p><a href="#"><strong>Comments ('.$num.')</strong></a> <span>&nbsp;&bull;&nbsp;</span>'.
                 date('Y-m-d H:i:s',$res[0]['ctime']).'</p>';
            echo '</div>';
        ?>
        <div class="article">
        <?php
            echo '<h2>'.$num.'Responses</h2>';
            echo '<div class="clr"></div>';
            foreach ($a as $k => $v) {
                echo '<div class="comment">';
                echo '<a href="#"><img src="/public/upload/'.$v['pic'].'" width="40" height="40" alt="" class="userpic" /></a>';
                echo '<p><a href="#">'.$v['nicname'].'</a> Says:<br />'.date('Y-m-d H:i:s',$v['ctime']).'</p>';
                echo '<p>'.$v['reply'].'</p>';
                echo '<p><a href="/home/blog.php/?id='.$res[0]['id'].'&p='.$reply[3].'&uid='.$v['uid'].'#message">回复</a></p>';
                echo '</div>';
            }
        ?>
        <br>
            <a href="<?php echo $_SERVER['PHP_SELF'] ?>?id=<?php echo $_GET['id']?>&p=1">首页</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] ?>?id=<?php echo $_GET['id']?>&p=<?php echo $reply[0]?>">上一页</a>
            <?php
                $leftRight = 1;
                $left = $reply[3] - $leftRight;
                $right = $reply[3] + $leftRight;
                if($left < 1){
                    $left = 1;
                    $right = $left + 2*$leftRight;
                }
                if($right > $reply[2]){
                    $right = $reply[2];
                    if($right > 2*$leftRight){
                        $left = $right - 2*$leftRight;
                    }else{
                        $left = 1;
                    }
                }

                if($left > 1){
                    echo '...';
                }

                for($i = $left;$i <= $right;$i++){
                    $color = ($i == $reply[3])?'style = "color:red;font-weight:900"':'';
                    echo '<a '.$color.' href="'.$_SERVER['PHP_SELF'].'?id='.$_GET['id'].'&p='.$i.'">'.$i.'</a>&nbsp;';
                }

                if($right < $reply[2]){
                    echo '...';
                }
            ?>
            <a href="<?php echo $_SERVER['PHP_SELF'] ?>?id=<?php echo $_GET['id']?>&p=<?php echo $reply[1]?>">下一页</a>
            <a href="<?php echo $_SERVER['PHP_SELF'] ?>?id=<?php echo $_GET['id']?>&p=<?php echo $reply[2]?>">尾页</a>
            <span>总共<?php echo $reply[2]?>页，当前：<?php echo $reply[3]?>/<?php echo $reply[2]?></span>
            <hr>
            <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method="get">
                跳转至：<input type="text" name="p">
                <input type="submit" value="go">
            </form>
        </div>
        <div class="article">
              <h2><span>Leave a</span> Reply</h2>
              <div class="clr"></div>
              <form action="/home/doaddreply.php" method="post" id="leavereply">
                <ol>
                  <li>
                    <input type="hidden" name="aid" value="<?php echo $_GET['id']?>">
                    <label for="message">Your Message</label>
                    <textarea id="message" name="reply" rows="8" cols="50"></textarea>
                  </li>
                  <li>
                    <input type="submit" value="submit">
                    <div class="clr"></div>
                  </li>
                </ol>
              </form>
            </div>
      </div>
<?php include_once('./public/footer.php');?>